package com.bhs.sansong.ui.orderDispose;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.PictureShowAdapter3;
import com.bhs.sansong.base.BaseFragment;
import com.bhs.sansong.bean.AbnormalOrderDetailBean;
import com.bhs.sansong.bean.OrderScheduleEvent;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.utils.DateUtils;
import com.bhs.sansong.utils.MyUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.util.SmartGlideImageLoader;

import org.greenrobot.eventbus.EventBus;

import java.util.Arrays;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 异常订单详情
 */
public class AbnormalOrderDetailFragment extends BaseFragment implements NetCallBack, View.OnClickListener {

    private int id;
    private TextView mTvComplaintTime;
    private TextView mTvDescribe;
    private RecyclerView mRvComplaint;
    private TextView mTvClient;
    private TextView mTvPhone;
    private TextView mTvType;
    private TextView mTvGoodsName;
    private TextView mTvDeliveryType;
    private TextView mTvDispatchingType;
    private TextView mTvOrderNumber;
    private TextView mTvDepartureAddress;
    private TextView mTvDeliveryAddress;
    private PictureShowAdapter3 showAdapter;

    public static AbnormalOrderDetailFragment newInstance(Bundle bundle) {
        AbnormalOrderDetailFragment fragment = new AbnormalOrderDetailFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_abnormal_order_detail;
    }

    @Override
    public void initView(View root) {
        id=getArguments().getInt("id");
        mTvComplaintTime = root.findViewById(R.id.tv_complaint_time);
        mTvDescribe = root.findViewById(R.id.tv_describe);
        mRvComplaint = root.findViewById(R.id.rv_complaint);
        mTvClient = root.findViewById(R.id.tv_client);
        mTvPhone = root.findViewById(R.id.tv_phone);
        mTvType = root.findViewById(R.id.tv_type);
        mTvGoodsName = root.findViewById(R.id.tv_goods_name);
        mTvDeliveryType = root.findViewById(R.id.tv_delivery_type);
        mTvDispatchingType = root.findViewById(R.id.tv_dispatching_type);
        mTvOrderNumber = root.findViewById(R.id.tv_order_number);
        mTvDepartureAddress = root.findViewById(R.id.tv_departure_address);
        mTvDeliveryAddress = root.findViewById(R.id.tv_delivery_address);
        mTvPhone.setOnClickListener(this);
        root.findViewById(R.id.iv_phone).setOnClickListener(this);
        loadingPopupShow();
        networkRequest(netApi.get_abnormal_info(id), this);
    }


    @Override
    public void initData() {

    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        loadingPopupDismiss();
        if (requestCode == 0) {
            AbnormalOrderDetailBean detailBean = new Gson().fromJson(json, AbnormalOrderDetailBean.class);
            if (detailBean.isStatus()) {
                EventBus.getDefault().postSticky(new OrderScheduleEvent(detailBean.getData().getHandle_log(),
                        detailBean.getData().getStatus(),detailBean.getData().getId(),detailBean.getData().getHandle_money()));
                if(!TextUtils.isEmpty(mTvDescribe.getText())){
                    return;
                }
                mTvDescribe.setText(detailBean.getData().getContent());
                mTvComplaintTime.setText(DateUtils.getTime(detailBean.getData().getCreate_time()));
                mTvClient.setText(detailBean.getData().getCargo_info().getNickname());
                mTvPhone.setText(detailBean.getData().getCargo_info().getMobile());
                mTvType.setText(detailBean.getData().getType());
                AbnormalOrderDetailBean.DataDTO.DeliverInfoDTO info = detailBean.getData().getDeliver_info();
                mTvGoodsName.setText(String.format("货物信息：%s/%s件/%s吨/%s立方", info.getGoods_name(), info.getGoods_number(),
                        MyUtils.checkPoint(info.getGoods_weight()), MyUtils.checkPoint(info.getGoods_volume())));
                mTvDeliveryType.setText(MyUtils.getDeliveryType(info.getJiaohuo_type()));
                mTvDispatchingType.setText(MyUtils.getDeliveryType(info.getPeisong_type()));
                mTvOrderNumber.setText(info.getDeliver_sn());
                if (TextUtils.isEmpty(info.getPickup_province())) {
                    mTvDepartureAddress.setText(String.format("%s-%s", info.getPickup_city(), info.getPickup_address()));
                } else {
                    mTvDepartureAddress.setText(String.format("%s-%s-%s", info.getPickup_province(), info.getPickup_city(), info.getPickup_address()));
                }

                if (TextUtils.isEmpty(info.getReceive_province())) {
                    mTvDeliveryAddress.setText(String.format("%s-%s", info.getReceive_city(), info.getReceive_address()));
                } else {
                    mTvDeliveryAddress.setText(String.format("%s-%s-%s", info.getReceive_city(), info.getReceive_city(), info.getReceive_address()));
                }
                if (!TextUtils.isEmpty(detailBean.getData().getImgs())) {
                    Object[] imgData = detailBean.getData().getImgs().split("\\|\\|");
                    showAdapter = new PictureShowAdapter3(R.layout.item_picture2, Arrays.asList(imgData));
                    showAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                            new XPopup.Builder(getContext())
                                    .asImageViewer(view.findViewById(R.id.iv_picture), position, Arrays.asList(imgData), null, new SmartGlideImageLoader())
                                    .isShowSaveButton(false)
                                    .show();
                        }
                    });
                    mRvComplaint.setAdapter(showAdapter);
                    mRvComplaint.setLayoutManager(new GridLayoutManager(getContext(), 3));
                }
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }

    @Override
    public void onClick(View view) {
        MyUtils.callPhone(getContext(),mTvPhone.getText().toString());
    }
}
